#include				"handler.h"
#include				"ASocket.h"

PacketHandler::PacketHandler(Processor & proc) : _proc(proc)
{

}

PacketHandler::~PacketHandler()
{

}

void					PacketHandler::harvest(ASocket *socket, boost::asio::ip::udp::endpoint *from)
{
  std::vector<byte>			*received = socket->getContent();
  Packet				*packet = toPacket(received);
  if (packet != NULL)
    {
      packet->setFrom(from->address().to_string());
      packet->setFrom(socket->getId());
      _proc.interpret(packet);
    }
}

void					PacketHandler::harvest(ASocket *socket)
{
  std::vector<byte>			*received = socket->getContent();
  Packet				*packet = toPacket(received);
  if (packet != NULL)
    {
      packet->setFrom(socket->getIp());
      packet->setFrom(socket->getId());
      _proc.interpret(packet);
    }
}

Packet					*PacketHandler::toPacket(byteArray * data) const
{
	Packet				*ret = new Packet;
	ret->setData(data);
	return ret;
}

void					PacketHandler::disconnect(ASocket *socket)
{
	_proc.disconnect(socket->getId());
}
